<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>WAF高级配置</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <script type="text/javascript" src="/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <blockquote class="layui-elem-quote layui-text">
            帮助文档及注意事项”：1. <a href="#" target="_blank">web安全功能说明</a> 2. <a href="https://wiki-dev.pc.com.cn/pages/viewpage.action?pageId=59219411" target="_blank">web安全配置</a>
            <a class="layui-btn" style="float:right" href="javascript:location.replace(location.href);" title="刷新">
              <i class="layui-icon">&#xe669;</i></a>
        </blockquote>
        <form class="layui-form layui-form-pane" action="" lay-filter="UpdateInfo">            
            <input type="hidden" name="id" readonly="true" value="" class="layui-input" disabled>

            <div class="layui-form-item" pane="">
                <label class="layui-form-label">日志模式</label>
                <div class="layui-input-block">
                    <input type="radio" name="log_model" value="kafka" lay-filter="log_model" title="kafka" checked="">   
                    <input type="radio" name="log_model" value="errorlog" lay-filter="log_model" title="error.log"> 
                    <input type="radio" name="log_model" value="file" lay-filter="log_model" title="file">                                                 
                </div>
            </div>
            <div class="layui-form-item" pane="">
                <label class="layui-form-label">kafka</label>
                <div class="layui-input-block" style="width: 300px;">   
                    <select name="kafka_broker_ip" id="kafka_broker_ip">
	              			<option value="192.168.237.197" selected="">192.168.237.197</option>
	              			<option value="192.168.237.173">192.168.237.173</option>
	              			<option value="192.168.237.174">192.168.237.174</option>
    		            </select>                                          
                </div>
            </div>

            <div class="layui-form-item" pane="">
                <label class="layui-form-label">URL长度</label>
                <div class="layui-input-inline"  style="width: 300px;">  
                    <input type="text" name="waf_url_length" placeholder="URL长度限制" value="4096" class="layui-input">
                </div>     
                <div class="layui-form-mid layui-word-aux">URL超出字符长度的则拒绝,默认4096,值为0时不限制</div>                            
            </div>
            <div class="layui-form-item" pane="">
                <label class="layui-form-label">恶意IP</label>
                <div class="layui-input-block"  style="width: 300px;"> 
                    <input type="radio" name="waf_riskip_model" value="deny" lay-filter="riskModel" title="拦截" checked="">     
                    <input type="radio" name="waf_riskip_model" value="reqlimit" lay-filter="riskModel" title="限流">                                                  
                </div> 
            </div>   
            <div id="riskModelTpl">        
              <div class="layui-form-item" pane="">
                  <label class="layui-form-label">限流阀值</label>
                  <div class="layui-input-inline"  style="width: 300px;">  
                      <input type="text" name="waf_riskip_ratev" placeholder="qps阀值" value="" class="layui-input">
                  </div>   
                  <div class="layui-form-mid layui-word-aux">qps阀值,需防刷状态为开启才会限流</div>                              
              </div>
            </div>

            <div class="layui-form-item" pane="">
                <label class="layui-form-label">排除cookie</label>
                <div class="layui-input-inline"  style="width: 300px;">  
                    <input type="text" name="exclude_cookie" placeholder="排除的cookie" value="" class="layui-input">
                </div>     
                <div class="layui-form-mid layui-word-aux">需要排除的cookie,多个cookie |隔开</div>                            
            </div>

            <div class="layui-collapse" lay-accordion="">
              <div class="layui-colla-item">
                <h2 class="layui-colla-title">web安全拦截模式</h2>
                <div class="layui-colla-content">
                    <div class="layui-form-item" pane="">
                        <label class="layui-form-label">拦截提示</label>
                        <div class="layui-input-block">
                            <input type="radio" name="waf_model" value="html" lay-filter="wafModel" title="html" checked="">
                            <input type="radio" name="waf_model" value="url" lay-filter="wafModel" title="redirect">  
                            <input type="radio" name="waf_model" value="deny" lay-filter="wafModel" title="封禁IP">                                                     
                        </div> 
                    </div> 
                    <div id="wafModelhtmlTpl">        
                      <div class="layui-form-item">
                          <label class="layui-form-label">html内容</label>
                          <div class="layui-input-inline">
                              <textarea name="waf_html" style="height:200px;width:600px" class="layui-textarea" lay-verify=""></textarea>
                          </div>                              
                      </div>
                    </div>     
                    <div id="wafModelUrlTpl">        
                      <div class="layui-form-item">
                          <label class="layui-form-label">url地址</label>
                          <div class="layui-input-inline">
                              <input type="text" name="waf_redirect_url" placeholder="url" value="" class="layui-input">
                          </div>
                                    
                      </div>
                    </div>   
                    <div id="wafModelTimeTpl">        
                      <div class="layui-form-item">
                          <label class="layui-form-label">封禁时长</label>
                          <div class="layui-input-inline">  
                              <input type="text" name="waf_expire_time" placeholder="封禁时长[秒]" value="" class="layui-input">
                          </div>                                 
                      </div>
                    </div>                
                </div>
              </div>
            </div>

            <div class="layui-collapse" lay-accordion="">
              <div class="layui-colla-item">
                <h2 class="layui-colla-title">限流自定义输出</h2>
                <div class="layui-colla-content">
                  <div class="layui-form-item" pane="">
                      <label class="layui-form-label">状态码</label>
                      <div class="layui-input-inline">
                          <input type="text" name="reqlimit_statuscode" placeholder="url" value="" class="layui-input">
                      </div> 
                  </div> 
  
                  <div class="layui-form-item" pane="">
                        <label class="layui-form-label">html内容</label>
                        <div class="layui-input-inline">
                            <textarea name="limit_html" style="height:200px;width:600px" class="layui-textarea" lay-verify=""></textarea>
                        </div> 
                  </div> 
                </div>
              </div>
            </div>  

            <div class="layui-form-item" style="margin-top:20px">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

            //页面初始化
        $("#riskModelTpl").hide();
        form.on('radio(riskModel)', function (data) { 
            if(data.value == 'reqlimit'){
              $("#riskModelTpl").show();
            }else{
              $("#riskModelTpl").hide();        
            }  
        });

        $("#wafModelUrlTpl").hide();
        $("#wafModelTimeTpl").hide();
        form.on('radio(wafModel)', function(data){
          if(data.value == 'html'){
              $("#wafModelhtmlTpl").show();
              $("#wafModelUrlTpl").hide();
              $("#wafModelTimeTpl").hide();
          }else if(data.value == 'url'){
              $("#wafModelhtmlTpl").hide();
              $("#wafModelUrlTpl").show();
              $("#wafModelTimeTpl").hide();
          }else{
              $("#wafModelhtmlTpl").hide();
              $("#wafModelUrlTpl").hide();
              $("#wafModelTimeTpl").show();        
          }
        });

			$.ajax({
			   url: "/xwaf/config/v1/GetOtherConfig" + location.search,
			   type: "GET",
			   headers:{'X-Requested-With':'XMLHttpRequest'},
			   success: function (resp) {
					if (resp.id > 0 ) {
  				  form.val('UpdateInfo', {
                  "id": resp.id
                  ,"appname": resp.appname
                  ,"kafka_broker_ip": resp.kafka_broker_ip
                  ,"log_model": resp.log_model
                  ,"waf_model": resp.waf_model
                  ,"waf_html": resp.waf_html
                  ,"waf_redirect_url": resp.waf_redirect_url
                  ,"waf_expire_time": resp.waf_expire_time
                  ,"waf_url_length": resp.waf_url_length
                  ,"waf_riskip_model": resp.waf_riskip_model
                  ,"waf_riskip_ratev": resp.waf_riskip_ratev
                  ,"reqlimit_statuscode": resp.reqlimit_statuscode
                  ,"limit_html": resp.limit_html
                  ,"exclude_cookie": resp.exclude_cookie
                  ,"updatetime": resp.updatetime     
  				  })

            if(resp.waf_riskip_model == 'reqlimit'){
              $("#riskModelTpl").show();
            }else{
              $("#riskModelTpl").hide();        
            } 

            if(resp.waf_model == 'html'){
              $("#wafModelhtmlTpl").show();
              $("#wafModelUrlTpl").hide();
              $("#wafModelTimeTpl").hide();
            }else if (resp.waf_model == 'url'){
              $("#wafModelhtmlTpl").hide();
              $("#wafModelUrlTpl").show();
              $("#wafModelTimeTpl").hide();
            }else{
              $("#wafModelhtmlTpl").hide();
              $("#wafModelUrlTpl").hide();
              $("#wafModelTimeTpl").show();        
            } 
 
          }else{
            layer.msg(resp.msg,{icon:2});
          }
				}
		    });	

        //监听提交
        form.on('submit(saveBtn)', function (datas) {
            var re = /^\d+$/;
            if (re.test(datas.field.waf_url_length)){
                console.log("ok");
            } else {
                layer.msg("url长度必须为大于等于0的整数!",{icon:2});
                return false;
            }
            if (re.test(datas.field.waf_expire_time)){
                console.log("ok");
            } else {
                layer.msg("禁止时长必须为大于0的整数!",{icon:2});
                return false;
            }
            datas.field.exclude_cookie = datas.field.exclude_cookie.replace(/^\s*|\s*$/g,"");
            datas.field.reqlimit_statuscode = datas.field.reqlimit_statuscode.replace(/^\s*|\s*$/g,""); //替换空格
            datas.field.limit_html = datas.field.limit_html.replace(/\\/g,""); //替换反斜杠
            datas.field.waf_redirect_url = datas.field.waf_redirect_url.replace(/^\s*|\s*$/g,""); //替换空格
            datas.field.waf_html = datas.field.waf_html.replace(/\\/g,""); //替换反斜杠
            layer.confirm('确定修改?', {icon: 3, title:'提示',yes: function(index){
                         $.ajax({
                           url: "/xwaf/config/v1/Update"+location.search,
                           type: "POST",
                           data: JSON.stringify(datas.field),
                           dataType: "json",
                           success: function (resp) {
    					                console.log(resp);
                               if(resp.code == 0){
                                   layer.msg('修改成功', {
                                              icon: 1,
                                              time: 2000
                                            },function(){
                                                window.location.reload();
                                    });  
                               }else{
                                   layer.msg('修改失败',{icon:2});
                               }
                            }
                         });		  	  
                   },
                    cancel: function(index, layero){ 
                        layer.close(index);
                    } 
            }); 	
            return false;
        });

    });
</script>
</body>
</html>